<template>
    <CreatedQuestion :close="() => { router.push({ name: '比赛题目' }) }" :getstart="asyncgetquestion" :ID="ID"
        :asynccreateorupdatequestion="asynccreateorupdatequestion" :istype="false" :isrange="false"
        :teams="$store.getters.getteam" :userid="$store.getters.getuser?.ID">
    </CreatedQuestion>
</template>

<script setup lang="ts">
import { computed, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "vuex";
import { asyncCreatedOrUpdateMatchQuestion } from "../../../api";
import { Storekey } from "../../../interface";
import Studycodemodel from "../../../mysql_interface/studycodemodel";
import CreatedQuestion from '../../components/createdquestion/index.vue';

const router = useRouter()
const store = useStore(Storekey)
const getuser = computed(() => {
    return store.state.user
})
const teamId = computed(() => {
    return getuser.value?.teamid;
});

async function asyncgetquestion() {
    const data = await asyncCreatedOrUpdateMatchQuestion({ teamid: teamId.value, ID: matchquestionid.value }) as Studycodemodel.Imatchquestion
    return Object.assign<Studycodemodel.Imatchquestion, Studycodemodel.Iquestion>(data, {
        testcases: data.example
    })
}

//
async function asynccreateorupdatequestion(body: Studycodemodel.Iquestion) {
    const data = await asyncCreatedOrUpdateMatchQuestion(
        Object.assign<Studycodemodel.Iquestion, Studycodemodel.Imatchquestion>(
            { teamid: teamId.value, ...body },
            { example: body.testcases }
        )
    ) as Studycodemodel.Imatchquestion
    return Object.assign<Studycodemodel.Imatchquestion, Studycodemodel.Iquestion>(data, {
        testcases: data.example
    })
}

const route = useRoute()

const ID = computed(() => {
    return Number(route.query.ID)
})
const matchquestionid = computed(() => {
    return Number(route.query.ID)
})

watch(() => matchquestionid.value, (v, o) => {
    if (!v) {
        router.push({ query: { ID: o } })
    }
})

function start() { }

</script>